<template>
    <div class="content">
        <div class="return" @click="close">
            <img class="img1" src="../../assets/img/return.png" alt="" />
            <p class="return-p">返回</p>
        </div>
        <div v-if="paytype == 1">
            <div class="card">
                <div class="card-title">充值须知</div>
                <div class="card-text">计费标准：1980元兑换1万元代金券。</div>
                <div class="grid">
                    <van-grid :column-num="3">
                        <van-grid-item icon="" text="A套餐" />
                        <van-grid-item icon="" text="B套餐" />
                        <van-grid-item icon="" text="C套餐" />
                    </van-grid>
                    <van-grid :column-num="3">
                        <van-grid-item icon="" text="加盟费2.98万元" />
                        <van-grid-item icon="" text="加盟费9990元" />
                        <van-grid-item icon="" text="加盟费0元" />
                    </van-grid>
                    <van-grid :column-num="3">
                        <van-grid-item icon="" text="盟主版永久会员" />
                        <van-grid-item icon="" text="企业版年度会员" />
                        <van-grid-item icon="" text="临时会员" />
                    </van-grid>
                    <van-grid :column-num="3">
                        <van-grid-item icon="" text="兑换比例13.8%" />
                        <van-grid-item icon="" text="兑换比例15.8%" />
                        <van-grid-item icon="" text="兑换比例19.8%" />
                    </van-grid>
                </div>
            </div>
            <div class="card">
                <div class="card-title">说明</div>
                <div class="card-text">1：永久会员充值1380元即可获得1万元代金券</div>
                <div class="card-text">2：年度会员充值1580元即可获得1万元代金券</div>
                <div class="card-text">3：临时会员充值1980元即可获得1万元代金券</div>
            </div>
            <div class="card">
                <div class="card-title">补充说明</div>
                <div class="card-text">1：每次充值不低于1380元人名币</div>
                <div class="card-text">2：充值账号低于1000元代金券时，系统会向客服及店长报警</div>
            </div>
            <div class="money-card">
                <div style="display: flex;">
                    <div style="flex:1" class="money-card-l">
                        请选择充值套餐
                    </div>
                    <div class="money-card-r">
                        代金券余额：<span style="color: red">{{ voucherNumber }}元</span>
                    </div>
                </div>
            </div>
            <div class="money-cards" v-for="(item, index) in moneyList" :key="index" @click="changeMoney(index)">
                <van-row type="flex">
                    <van-col span="6" class="center">{{ item.name }}</van-col>
                    <van-col span="6" class="center"> <span style="color: red;">{{ item.money }}</span></van-col>
                    <van-col span="8" class="center">{{ item.type }}</van-col>
                    <van-col span="4" class="centers"><van-radio-group v-model="radio" icon-size="15px">
                            <van-radio :name="index" checked-color="#ee0a24"></van-radio>
                        </van-radio-group></van-col>
                </van-row>
            </div>

        </div>
        <div v-if="paytype == 2">
            <!-- 缴纳诚意金 -->
            <div class="card">
                <div class="card-title">缴纳诚意金须知</div>
                <div class="card-text"> 1：为了杜绝无诚意的合作，特此设立诚意金。</div>
                <div class="card-text"> 2：诚意金1万元，且不得在合作中途退出。该诚意金本金，可在合作期满+1天退还店长。</div>
                <div class="card-text">3：<span style="color: red"> 不交诚意金的店长，不能扫码送券。</span> 30天后，将被视为自动放弃，将被系统清零。</div>
            </div>
            <div class="money-card">
                <div style="display: flex;">
                    <div style="flex:1" class="money-card-l">
                        请选择充值套餐
                    </div>
                </div>
            </div>
            <div class="money-cards" v-for="(item, index) in sincerityList" :key="index" @click="sincerityMoney(index)">
                <van-row type="flex">
                    <van-col span="6" class="center">{{ item.name }}</van-col>
                    <van-col span="6" class="center"> <span style="color: red;">{{ item.money }}</span></van-col>
                    <van-col span="6" class="center">{{ item.type }}</van-col>
                    <van-col span="6" class="centers"><van-radio-group v-model="radio1" icon-size="15px">
                            <van-radio :name="index" checked-color="#ee0a24"></van-radio>
                        </van-radio-group></van-col>
                </van-row>
            </div>
        </div>
        <div @click="payFn" class="button-pay">
            <van-loading style="color: #fff;" v-if="loadings" size="24px"><span
                    style="color: #fff;">支付中...</span></van-loading>
            <span v-else> 支付</span>
        </div>
    </div>
</template>
  
<script>
import { pay, checkpay, getMemberType } from "@/api/fulfil.js";
export default {
    name: 'payIndex',
    data() {
        return {
            moneyList: [
                {
                    name: 'A套餐',
                    money: '1380元',
                    type: '盟主版永久会员',
                    price: '138000',
                },
                {
                    name: 'B套餐',
                    money: '1580元',
                    type: '企业版年度会员',
                    price: '158000',
                },
                {
                    name: 'C套餐',
                    money: '1980元',
                    type: '临时会员',
                    price: '198000',
                }
            ],
            sincerityList: [
                {
                    name: '诚意金',
                    money: '100元',
                    type: '微信',
                    price: '10000',
                },
            ],
            radio: '',//余额充值
            radio1: '',//诚意金
            loadings: false,
            voucherNumber: '',
            paytype: '',//1充代金券 2充余额
            payType:'',//充值类型 1 2 3 店长必填
            payee:'',//选择充值的金额
        };
    },
    created() {

    },
    mounted() {

    },
    activated() {
        this.radio = '';
        this.radio1 = '';
        this.loadings = false;
        this.payee = '';
        this.paytype = localStorage.getItem('paytype'); //1充值代金券  2充诚意金
        this.voucherNumber = localStorage.getItem("voucherNumber");
    },
    methods: {
        //获取会员类型
        getMember() {
            getMemberType({
                agentid: localStorage.getItem("agentid")
            })
                .then((res) => {
                    console.log(res, 'res');
                    this.moneyList = res.data.data;
                    console.log(this.memberList, 'this.memberList');
                })
                .catch((error) => {
                    console.log(error);
                });
        },
        //支付完回调查询
        checkPayFn(orderid) {
            checkpay({
                orderid: orderid,
                agentid: localStorage.getItem("agentid"),
            })
                .then((res) => {
                    console.log(res, 'res222');
                    if (res.data.code == 0) {
                        this.$Toast("充值成功");
                        window.history.go(-1);
                    } else {
                        this.$Toast(res.data.msg);
                    }
                    this.loadings = false;
                })
                .catch((error) => {
                    this.loadings = false;
                    console.log(error);
                });


        },
        //选择套餐
        changeMoney(index) {
            this.radio = index;
            this.payType = (index+1).toString();
            this.payee = this.moneyList[index].price;
            console.log(this.radio, 'radio');
        },
        //选择套餐
        sincerityMoney(index) {
            this.radio1 = index;
            console.log(this.radio, 'radio');
            this.payee = this.sincerityList[index].price;
        },
        // 获取用户id
        getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        },
        //支付
        payFn() {
            if (this.paytype == 1) {
                if (!this.radio && this.radio !== 0) {
                    this.$Toast("请先选择要充值的套餐");
                    return false;
                }
            } else if (this.paytype == 2) {
                console.log(!this.radio1, this.radio1 != 0, 'this.radio1');
                if (!this.radio1 && this.radio1 !== 0) {
                    this.$Toast("请先选择要充值的套餐");
                    return false;
                }
            }
            this.loadings = true;
            let agentid = localStorage.getItem("agentid");
            let openid = localStorage.getItem("openid");
            console.log(agentid, 'agentid', openid, 'openid');
            let that = this;
            pay({
                agentid: localStorage.getItem("agentid"),
                openid: localStorage.getItem("openid"),//
                payee: that.payee,
                type: that.paytype,
                payType:that.payType,
            })
                .then((res) => {
                    console.log(res, 'res.code支付');
                    if (res.data.code == 0) {
                        wx.config({
                            debug: false, // 开启调试模式，仅在pc端时才会打印。
                            appId: 'wxd7fdd57dae07f2f3', // 必填，公众号的唯一标识
                            timestamp: res.data.data.timeStamp, // 必填，生成签名的时间戳
                            nonceStr: res.data.data.nonceStr, // 必填，生成签名的随机串
                            signature: res.data.data.paySign, // 必填，签名
                            jsApiList: ['chooseWXPay'] // 必填，需要使用的JS接口列表
                        })
                        // 调用支付
                        wx.ready(function () {
                            wx.chooseWXPay({
                                timestamp: res.data.data.timeStamp, // 时间戳
                                nonceStr: res.data.data.nonceStr, // 随机数
                                package: res.data.data.package, //订单详情扩展字符串
                                signType: 'MD5',
                                paySign: res.data.data.paySign, // 签名
                                success: function () {
                                    that.checkPayFn(res.data.data.orderid);
                                },
                                cancel: function () {
                                    that.$Toast("取消支付")
                                    that.loadings = false;
                                },
                                fail: function () {
                                    that.$Toast("支付失败")
                                    that.loadings = false;
                                }
                            })
                        })
                    } else if (res.data.code == -2) {
                        if(that.payType == 1){
                            that.$Toast("很抱歉，您还不是永久会员，请联络您的客服经理或社长。或者选择其他项继续充值。");
                        }else  if(that.payType == -2){
                            that.$Toast("很抱歉，您还不是年度会员，请联络您的客服经理或社长。或者选择其他项继续充值。");
                        }
                        that.loadings = false;
                    } else {
                        that.$Toast(res.data.msg);
                        that.loadings = false;
                    }
                })
                .catch((error) => {
                    console.log(error);
                });

        },
        close() {
            window.history.go(-1);
        },
    },
};
</script>
  
<style scoped>
.content {
    padding-bottom: 2rem;
    position: relative;
    overflow: hidden;
    width: 100%;
    min-height: 100vh;
    background: #e1f7fc;
}

.button-pay {
   /*  margin: .3rem auto 0; */
    position: fixed;
    left: 10%;
    bottom: .1rem;
    width: 80%;
    height: 1rem;
    line-height: 1rem;
    text-align: center;
    border-radius: .5rem;
    /* background: linear-gradient(to right, #ff1c40, #ff6319); */
    background-color: #ff6319;
    color: #fde7e1;
    font-size: .375rem;
}

.img1 {
    display: inline-block;
    width: 27px;
    height: 48px;
    vertical-align: middle;
}

.return {
    margin: 20px 0 0 30px;
    font-size: 40px;
}

.return-p {
    margin-left: .25rem;
    display: inline-block;
    vertical-align: middle;
}

.card {
    box-sizing: border-box;
    overflow: hidden;
    margin: .3rem auto;
    width: 80%;
    background-color: #fff;
    border-radius: .3rem;
    padding: .3rem;
}

.money-card {
    box-sizing: border-box;
    overflow: hidden;
    margin: .3rem auto;
    width: 80%;
    background-color: #fff;
    border-radius: .3rem;
    padding: .3rem;
}

.money-cards {
    box-sizing: border-box;
    overflow: hidden;
    margin: .2rem auto;
    width: 80%;
    background-color: #fff;
    border-radius: .3rem;
    padding: .3rem;
    font-size: .35rem;
}

::v-deep .van-grid-item__text {
    font-size: .36rem;
    color: #000;
}

.card-title {
    font-size: .4rem;
    font-weight: 600;
    color: #000;
}

.money-card-l {
    font-size: .4rem;
    font-weight: 600;
    color: #000;
}

.money-card-r {
    font-size: .4rem;
    font-weight: 600;
    color: #000;
}

.card-text {
    margin-top: .2rem;
    font-size: .35rem;
    font-weight: 500;
    color: #000;
}

.grid {
    width: 100%;
    height: 2rem;
    overflow-y: auto;
    margin-top: .2rem;
}

.center {
    text-align: center;
}

.centers {
    padding-left: .4rem;
}
</style>